#include "seqlist.h"
 
void SLprint(SL* ps)
{
	assert(ps);
	for (int i = 0;i < ps->size;++i)
	{
		printf("%d", ps->a[i]);
	}
	printf("\n");
}

void SLCheckCapacity(SL* ps)
{
	assert(ps);
	if (ps->size == ps->capecity)
	{
		SLdatetype* tmp = (SLdatetype*)realloc(ps->a, sizeof(SLdatetype) * ps->capecity * 2);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->a = tmp;
		ps->capecity *= 2;
	}
}

void SLInit(SL* ps)
{
	assert(ps);
	ps->a = (SLdatetype*)malloc(sizeof(SLdatetype)*INIT_Capacity);
	if (ps->a == NULL)
	{
		perror("malloc fail");
		return;
	}
	//assert(ps->a);
	ps->size = 0;
	ps->capecity = INIT_Capacity;
	
}
void SLDestroy(SL* ps)
{
	assert(ps);
	free(ps->a); 
	ps->a = NULL;
	ps->capecity = ps->size = 0; 
}
void SLPushBack(SL* ps, SLdatetype x)
{
	assert(ps);
	void SLCheckCapacity(ps); 
	ps->a[ps->size] = x;
	ps->size++;
}
void SLPopback(SL* ps)
{
	assert(ps->a);  
	ps->size--;
}
void SLPushFront(SL* ps, SLdatetype x)
{
	assert(ps);
	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}
	ps->a[0] = x;
	ps->size++;

}
void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size > 0); 
	int begin = 1;
	while (begin < ps->size)
	{
		ps->a[begin - 1] = ps->a[begin];
		++begin;
	}

	ps->size--;
}
void SLInsert(SL* ps, int pos, SLdatetype x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);

	SLCheckCapacity(&ps);

	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}
	ps->a[pos] = x;
	ps->size++;
}

void SLErase(SL* ps, int pos)
{
	assert(ps);

	assert(pos >= 0 && pos < ps->size);

	int begin = pos + 1;
	while (begin < ps->size)
	{
		ps->a[begin - 1] = ps->a[begin];
		++begin;
	}
	ps->size--;
}
